package net.message;

import net.message.processor.IMessageProcessor;
import net.Client;
import net.Util;

import java.util.Set;
import java.util.HashSet;

public class HelloResponseMessage implements Message{

    public final Set<Integer> imageIDs;

    public HelloResponseMessage(Set<Integer> imageIDs) {
        this.imageIDs = imageIDs;
    }

    public HelloResponseMessage(byte[] source) {
        imageIDs = new HashSet<Integer>();
        for (int i = 0; i < source.length / 4; i++) {
            imageIDs.add(Util.byteArrayToInt(source, i * 4));
        }
    }


    @Override
    public void accept(IMessageProcessor processor, Client client) {
        processor.process(this, client);
    }

    @Override
    public byte[] toByteArray() {
        byte[] res = new byte[imageIDs.size() * 4];
        int k = 0;
        for (Integer imageID: imageIDs) {
            Util.intToByteArray(imageID, res, k);
            k += 4;
        }
        return res;
    }

    @Override
    public MessageType getType(){
        return MessageType.HELLO_RESPONSE;
    }


}
